package com.qike.duola.service.impl;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.google.common.base.Joiner;
import com.qike.duola.socket.NewPlanOrderSocket;
import com.qike.duola.domain.*;
import com.qike.duola.dto.*;
import com.qike.duola.enums.*;
import com.qike.duola.mapper.*;
import com.qike.duola.service.DuolaPlanService;
import com.qike.duola.utils.ChineseCharToEnUtil;
import com.qike.duola.utils.TimeTool;
import com.qike.duola.vo.*;
import org.apache.shiro.SecurityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.io.IOException;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.*;

/**
 * @Author: Eric.zhou
 * @Date: 2020/9/2 15:10
 */
@Service
public class DuolaPlanServiceImpl implements DuolaPlanService {
    private static final Logger logger = LoggerFactory.getLogger(DuolaPlanServiceImpl.class);

    @Autowired
    private DuolaPlanMapper duolaPlanMapper;
    @Autowired
    private DuolaTransOrderMapper duolaTransOrderMapper;
//    @Autowired
//    private DuolaTransOrderService duolaTransOrderService;
    @Autowired
    private DuolaGoodsMapper duolaGoodsMapper;
    @Autowired
    private DuolaFormulaMapper duolaFormulaMapper;
    @Autowired
    private DuolaProjectMapper duolaProjectMapper;
    @Autowired
    private DuolaContractMapper duolaContractMapper;
    @Autowired
    private SysUserMapper userMapper;
    @Autowired
    private DuolaCarMapper duolaCarMapper;
    @Autowired
    private DuolaCustomerMapper duolaCustomerMapper;

    @Autowired
    private NewPlanOrderSocket newPlanOrderSocket;
//    @Autowired
//    private SocketService socketService;


    /**
     * 添加生产计划
     * @param planAddDto
     */
    @Override
    public void planAdd(PlanAddDto planAddDto) throws Exception {
        Long userId = (Long) SecurityUtils.getSubject().getPrincipal();
        logger.info(userId+"添加生产计划"+ JSON.toJSON(planAddDto));
        DuolaPlan duolaPlan = new DuolaPlan();
        BeanUtils.copyProperties(planAddDto,duolaPlan);
        duolaPlan.setPlanTime(TimeTool.LongToLocalDateTime(planAddDto.getPlanTime()));
        duolaPlan.setAddTime(LocalDateTime.now());
        DuolaProject duolaProject = duolaProjectMapper.selectByPrimaryKey(planAddDto.getProjectId());
        if (duolaProject==null){
            throw new Exception(ErrorEnums.ERROR_DATA.getEmsg());
        }
        duolaPlan.setProjectShortName(ChineseCharToEnUtil.getFirstSpell(duolaProject.getProjectName()));
        duolaPlan.setProjectName(duolaProject.getProjectName());
        duolaPlan.setIsSpecial(planAddDto.getIsSpecial());
        DuolaContractExample  example = new DuolaContractExample();
        example.createCriteria().andProjectIdEqualTo(planAddDto.getProjectId()).andContractTypeNotEqualTo(ContractTypeEnums.SUPPLEMENTARY_AGREEMENT.getEcode());
        List<DuolaContract> duolaContracts = duolaContractMapper.selectByExample(example);
        if (duolaContracts.size()<=0){
            throw new Exception(ErrorEnums.ERROR_DATA.getEmsg());
        }
        DuolaContract duolaContract = duolaContracts.get(0);
        duolaPlan.setAddUid(userId);
        duolaPlan.setPouringTime(TimeTool.LongToLocalDateTime(planAddDto.getPouringTime()));
        duolaPlan.setContractId(duolaContract.getId());
        duolaPlan.setStatus(StatusEnums.NO.getEcode());
        if (planAddDto.getPumpArriveTime()!=null){
            duolaPlan.setPumpArriveTime(TimeTool.LongToLocalDateTime(planAddDto.getPumpArriveTime()));
        }
        duolaPlan.setOrderStatus(PlanOrderStatusEnums.NOT_SEND.getEcode());
        duolaPlanMapper.insertSelective(duolaPlan);
        //发送新订单长连接
        try {
            newPlanOrderSocket.sendMessage(JSON.toJSONString(planList(null,null, null)));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 生产计划列表
     *
     * @param projectName
     * @param orderStatus
     * @return
     */
    @Override
    public List<PlanListVo> planList(String projectName, Long time, Integer orderStatus) {
        Long userId = (Long) SecurityUtils.getSubject().getPrincipal();
        logger.info(userId+"生产计划列表"+ JSON.toJSON(projectName));
        LocalDate now = LocalDate.now();
        if (time!=null){
            now = TimeTool.LongToLocalDate(time);
        }
        DuolaPlanExample example = new DuolaPlanExample();
        List<PlanListVo> listVos = new ArrayList<>();
        Map<Integer,String> goodsName = new HashMap<>();
        Map<Integer,String> carMap = new HashMap<>();
        //获取所有车辆
        List<DuolaCar> duolaCars = duolaCarMapper.selectByExample(new DuolaCarExample());
        for (DuolaCar duolaCar : duolaCars) {
            carMap.put(duolaCar.getId(),duolaCar.getCarNo());
        }
        //获取所有运输单
        //获取所有工程信息
        Map<Integer,DuolaProject> projectMap = new HashMap<>();
        DuolaProjectExample projectExample = new DuolaProjectExample();
        projectExample.createCriteria().andExamineStatusEqualTo(ExamineEnums.EXAMINE_SUCCESS.getEcode()).andStatusEqualTo(StatusEnums.NO.getEcode());
        List<DuolaProject> duolaProjects = duolaProjectMapper.selectByExample(projectExample);
        for (DuolaProject duolaProject : duolaProjects) {
            projectMap.put(duolaProject.getId(),duolaProject);
        }
        //获取所有运输单
        Map<Integer,Double> transList = new HashMap<>();
        DuolaTransOrderExample transOrderExample = new DuolaTransOrderExample();
        transOrderExample.createCriteria().andOrderStatusEqualTo(TransOrderStatusEnums.FINISH.getEcode()).andStatusEqualTo(StatusEnums.NO.getEcode());
        List<DuolaTransOrder> duolaTransOrders = duolaTransOrderMapper.selectByExample(transOrderExample);
        for (DuolaTransOrder duolaTransOrder : duolaTransOrders) {
            if (transList.get(duolaTransOrder.getPlanId())==null){
                transList.put(duolaTransOrder.getPlanId(),duolaTransOrder.getSignVolume());
            }else {
                transList.put(duolaTransOrder.getPlanId(),transList.get(duolaTransOrder.getPlanId())+duolaTransOrder.getSignVolume());
            }
        }
        //获取所有物料 非基础原料
        DuolaGoodsExample goodsExample = new DuolaGoodsExample();
        goodsExample.createCriteria().andGoodsTypeNotEqualTo(GoodsTypeEnums.PRODUCT.getEcode());
        List<DuolaGoods> duolaGoods = duolaGoodsMapper.selectByExample(goodsExample);
        for (DuolaGoods duolaGood : duolaGoods) {
            goodsName.put(duolaGood.getId(),duolaGood.getGoodsName());
        }
        DuolaPlanExample.Criteria criteria = null;
        if (!StringUtils.isEmpty(projectName)){
            criteria= example.or().andPlanTimeBetween(LocalDateTime.of(now, LocalTime.MIN), LocalDateTime.of(now, LocalTime.MAX)).andStatusEqualTo(StatusEnums.NO.getEcode()).andProjectNameLike(projectName);
            criteria =  example.or().andPlanTimeBetween(LocalDateTime.of(now, LocalTime.MIN), LocalDateTime.of(now, LocalTime.MAX)).andStatusEqualTo(StatusEnums.NO.getEcode()).andProjectShortNameLike(projectName);
        }else {
            criteria = example.createCriteria().andPlanTimeBetween(LocalDateTime.of(now, LocalTime.MIN), LocalDateTime.of(now, LocalTime.MAX)).andStatusEqualTo(StatusEnums.NO.getEcode());
        }
        if (orderStatus!=null){
            criteria.andOrderStatusEqualTo(orderStatus);
        }else {
            criteria.andOrderStatusNotEqualTo(PlanOrderStatusEnums.NOT_SEND.getEcode());
        }

        List<DuolaPlan> duolaPlans = duolaPlanMapper.selectByExample(example);
        for (DuolaPlan duolaPlan : duolaPlans) {
            PlanListVo vo = new PlanListVo();
            BeanUtils.copyProperties(duolaPlan,vo);
            //todo 已发货数量
            vo.setSendVolume(this.getSendVolumeByProjectId(duolaPlan.getProjectId()));
            vo.setGoodsName(goodsName.get(duolaPlan.getGoodsId()));
            if (!StringUtils.isEmpty(duolaPlan.getSpecialId())){
                List<String> result = Arrays.asList(duolaPlan.getSpecialId().split(","));
                List<String> specialNames = new ArrayList<>();
                for (String s : result) {
                    specialNames.add(goodsName.get(Integer.valueOf(s)));
                }
                String str = Joiner.on(",").join(specialNames);
                vo.setSpecialNames(str);
            }
            if (duolaPlan.getPumpArriveTime()!=null){
                vo.setPumpArriveTime(TimeTool.LocalDateTimeFormat(duolaPlan.getPumpArriveTime()));
            }
            vo.setOrderStatus(duolaPlan.getOrderStatus());
            vo.setPlanVolume(duolaPlan.getPlanVloume());
            if (carMap.get(duolaPlan.getPumpCarId())!=null){
                vo.setPumpCar(carMap.get(duolaPlan.getPumpCarId()));
            }
            //工程经纬度
            vo.setLatitude(projectMap.get(duolaPlan.getProjectId()).getLatitude());
            vo.setLongitude(projectMap.get(duolaPlan.getProjectId()).getLongitude());

            listVos.add(vo);
        }

        return listVos;
    }

    public Double getSendVolumeByProjectId(Integer id) {
        Double total = new Double(0);
        DuolaTransOrderExample example = new DuolaTransOrderExample();
        example.createCriteria().andProjectIdEqualTo(id).andOrderStatusGreaterThanOrEqualTo(TransOrderStatusEnums.TRANS_ING.getEcode()).andStatusEqualTo(StatusEnums.NO.getEcode());
        List<DuolaTransOrder> transOrders = duolaTransOrderMapper.selectByExample(example);
        for (DuolaTransOrder transOrder : transOrders) {
            if (transOrder.getSignVolume()!=null){
                total=total+transOrder.getSignVolume();
            }
        }
        return total;
    }

    /**
     * 生产计划详情/编辑跳转
     *
     * @param id
     * @return
     */
    @Override
    public PlanEditJumpVo planEditJump(Integer id) throws Exception {
        Long userId = (Long) SecurityUtils.getSubject().getPrincipal();
        logger.info(userId+"生产计划详情/编辑跳转"+ JSON.toJSON(id));
        DuolaPlan duolaPlan = duolaPlanMapper.selectByPrimaryKey(id);
        if (duolaPlan==null){
            throw new Exception(ErrorEnums.ERROR_DATA.getEmsg());
        }
        PlanEditJumpVo vo = new PlanEditJumpVo();
        BeanUtils.copyProperties(duolaPlan,vo);
        DuolaCustomer duolaCustomer = duolaCustomerMapper.selectByPrimaryKey(duolaPlan.getCustomerId());
        if (duolaCustomer!=null){
            vo.setCustomerName(duolaCustomer.getCustomerName());
        }
        if (duolaPlan.getPumpCarId()!=null){
            DuolaCar car = duolaCarMapper.selectByPrimaryKey(duolaPlan.getPumpCarId());
            if (car!=null){
                vo.setPumpCarNo(car.getCarNo());
            }
        }
        vo.setIsSpecial(duolaPlan.getIsSpecial());
        vo.setPlanTime(TimeTool.LocalDateTimeToLong(duolaPlan.getPlanTime()));
        vo.setPumpArriveTime(TimeTool.LocalDateTimeToLong(duolaPlan.getPumpArriveTime()));
        vo.setPouringTime(TimeTool.LocalDateTimeToLong(duolaPlan.getPouringTime()));
        return vo;
    }

    /**
     * 生产计划编辑
     *
     * @param planEditDto
     */
    @Override
    public void planEdit(PlanEditDto planEditDto) throws Exception {
        Long userId = (Long) SecurityUtils.getSubject().getPrincipal();
        logger.info(userId+"生产计划编辑"+ JSON.toJSON(planEditDto));
        DuolaPlan duolaPlan = duolaPlanMapper.selectByPrimaryKey(planEditDto.getId());
        if (duolaPlan==null){
            throw new Exception(ErrorEnums.ERROR_DATA.getEmsg());
        }
        BeanUtils.copyProperties(planEditDto,duolaPlan);
        duolaPlan.setPlanTime(TimeTool.LongToLocalDateTime(planEditDto.getPlanTime()));
        duolaPlan.setPumpArriveTime(TimeTool.LongToLocalDateTime(planEditDto.getPumpArriveTime()));
        duolaPlan.setPouringTime(TimeTool.LongToLocalDateTime(planEditDto.getPouringTime()));
        duolaPlan.setIsSpecial(planEditDto.getIsSpecial());
        duolaPlanMapper.updateByPrimaryKeySelective(duolaPlan);
    }

    /**
     * 生产计划完成
     *
     * @param planFinishDto
     */
    @Override
    public void planFinish(PlanFinishDto planFinishDto) throws Exception {
        Long userId = (Long) SecurityUtils.getSubject().getPrincipal();
        logger.info(userId+"生产计划完成"+ JSON.toJSON(planFinishDto));
        DuolaPlan duolaPlan = duolaPlanMapper.selectByPrimaryKey(planFinishDto.getId());
        if (duolaPlan==null){
            throw new Exception(ErrorEnums.ERROR_DATA.getEmsg());
        }
        //查询是否有未完成的运输单
        DuolaTransOrderExample example = new DuolaTransOrderExample();
        example.createCriteria().andPlanIdEqualTo(planFinishDto.getId()).andOrderStatusNotEqualTo(TransOrderStatusEnums.FINISH.getEcode());
        List<DuolaTransOrder> duolaTransOrders = duolaTransOrderMapper.selectByExample(example);
        if (duolaTransOrders.size()>0){
            throw new Exception(ErrorEnums.CANT_FINISH.getEmsg());
        }
        duolaPlan.setOrderStatus(PlanOrderStatusEnums.FINISH.getEcode());
        duolaPlanMapper.updateByPrimaryKeySelective(duolaPlan);
        //查询这些未完成
        //发送新订单长连接
        try {
            newPlanOrderSocket.sendMessage(JSON.toJSONString(planList(null,null, null)));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 获取所有车辆列表
     *
     * @param planId
     * @return
     */
    @Override
    public List<GetCarListVo> getCarList(Integer planId) {
        List<GetCarListVo> list = new ArrayList<>();
        //查询出所有已完成的运输单
        DuolaTransOrderExample transOrderExample = new DuolaTransOrderExample();
        DuolaTransOrderExample.Criteria criteria = transOrderExample.createCriteria();
        if (planId!=null){
            criteria.andPlanIdEqualTo(planId);
        }
//        criteria.andOrderStatusEqualTo(TransOrderStatusEnums.FINISH.getEcode());
        List<DuolaTransOrder> duolaTransOrders = duolaTransOrderMapper.selectByExample(transOrderExample);
        Map<Integer,Double> carTotalMap = new HashMap<>();
        for (DuolaTransOrder duolaTransOrder : duolaTransOrders) {
            carTotalMap.put(duolaTransOrder.getCarId(),carTotalMap.get(duolaTransOrder.getCarId())==null?new Double(0):carTotalMap.get(duolaTransOrder.getCarId())+(duolaTransOrder.getTotalVolume()==null?new Double(0):duolaTransOrder.getTotalVolume()));
        }
        List<Integer> carIds =  Arrays.asList( carTotalMap.keySet().toArray(new Integer[carTotalMap.keySet().size()]) );
        //查询出所有车辆
        DuolaCarExample carExample = new DuolaCarExample();

        DuolaCarExample.Criteria criteria1 = carExample.createCriteria().andStatusEqualTo(StatusEnums.NO.getEcode());
        if (planId!=null){
            criteria1.andIdIn(carIds);
        }
        if (carIds.size()<=0){
            return list;
        }
        List<DuolaCar> duolaCars = duolaCarMapper.selectByExample(carExample);
        for (DuolaCar duolaCar : duolaCars) {
            GetCarListVo vo = new GetCarListVo();
            vo.setCarId(duolaCar.getId());
            vo.setCapacity(duolaCar.getCapacity());
            vo.setCarStatus(duolaCar.getTransStatus());
            if (duolaCar.getTransStatus()!=null){
                vo.setCarStatusStr(CarTransStatusEnums.statOf(duolaCar.getTransStatus()).getEmsg());
            }
            vo.setCarType(duolaCar.getCarType());
            vo.setLatitude(duolaCar.getLatitude());
            vo.setCarNo(duolaCar.getCarNo());
            vo.setLongitude(duolaCar.getLongitude());
//            vo.setSpeed(duolaCar.getSimNo());

            vo.setHistoryTotal(carTotalMap.get(duolaCar.getId())==null?new Double(0):carTotalMap.get(duolaCar.getId()));
            list.add(vo);
        }
        return list;
    }

    /**
     * 生产计划列表
     *
     * @param planManageListDto
     * @return
     */
    @Override
    public PlanManageListVo planManageList(PlanManageListDto planManageListDto) {
        PlanManageListVo vo = new PlanManageListVo();
        List<PlanManageList> list = new ArrayList<>();
        Long userId = (Long) SecurityUtils.getSubject().getPrincipal();
        DuolaPlanExample example = new DuolaPlanExample();
        DuolaPlanExample.Criteria criteria = example.createCriteria();
        criteria.andStatusEqualTo(StatusEnums.NO.getEcode());
        criteria.andOrderStatusEqualTo(PlanOrderStatusEnums.NOT_SEND.getEcode());
        if (planManageListDto.getProjectId()!=null){
            criteria.andProjectIdEqualTo(planManageListDto.getProjectId());
        }
        if (planManageListDto.getPlanTime()!=null){
            LocalDate localDate = TimeTool.LongToLocalDate(planManageListDto.getPlanTime());
            criteria.andPlanTimeBetween(TimeTool.getDayBeginLocalDateTime(localDate),TimeTool.getDayEndLocalDateTime(localDate));
        }else {
            criteria.andPlanTimeBetween(TimeTool.getDayBeginLocalDateTime(null),TimeTool.getDayEndLocalDateTime(null));
        }
        //查询所有客户
        List<DuolaCustomer> duolaCustomers = duolaCustomerMapper.selectByExample(new DuolaCustomerExample());
        Map<Integer,String> customerMap = new HashMap<>();
        for (DuolaCustomer duolaCustomer : duolaCustomers) {
            customerMap.put(duolaCustomer.getId(),duolaCustomer.getCustomerName());
        }
        //查询所有物料
        List<DuolaGoods> duolaGoods = duolaGoodsMapper.selectByExample(new DuolaGoodsExample());
        Map<Integer,String> GoodsMap = new HashMap<>();
        for (DuolaGoods goods : duolaGoods) {
            GoodsMap.put(goods.getId(),goods.getGoodsName());
        }
        //查询所有车辆
        List<DuolaCar> duolaCars = duolaCarMapper.selectByExample(new DuolaCarExample());
        Map<Integer,String> CarMap = new HashMap<>();
        for (DuolaCar car : duolaCars) {
            CarMap.put(car.getId(),car.getCarNo());
        }
        Page page = PageHelper.startPage(planManageListDto.getStartPage(), planManageListDto.getPageSize());
        List<DuolaPlan> duolaPlans = duolaPlanMapper.selectByExample(example);
        for (DuolaPlan duolaPlan : duolaPlans) {
            PlanManageList planManageList = new PlanManageList();
            planManageList.setId(duolaPlan.getId());
            planManageList.setOrderStatus(duolaPlan.getOrderStatus());
            planManageList.setContacts(duolaPlan.getContacts());
            planManageList.setContactsPhone(duolaPlan.getContactsPhone());
            planManageList.setPlanVloume(duolaPlan.getPlanVloume());
//            planManageList.setCustomerName(duolaPlan.getCustomerId());
            planManageList.setCustomerName(customerMap.get(duolaPlan.getCustomerId()));
            planManageList.setGoodsName(GoodsMap.get(duolaPlan.getGoodsId()));
            planManageList.setPart(duolaPlan.getPart());
            planManageList.setPouringTime(TimeTool.LocalDateTimeFormat(duolaPlan.getPouringTime()));
            planManageList.setProjectName(duolaPlan.getProjectName());
            planManageList.setPumpArriveTime(TimeTool.LocalDateTimeFormat(duolaPlan.getPumpArriveTime()));
            planManageList.setSendVolume(0.0);
            planManageList.setPumpCarName(CarMap.get(duolaPlan.getPumpCarId()));
            StringBuilder specialNames = new StringBuilder();
            if (!StringUtils.isEmpty(duolaPlan.getSpecialId())){
                String[] split = duolaPlan.getSpecialId().split(",");
                for (String s : split) {
                    String s1 = GoodsMap.get(Integer.valueOf(s));
                    if (s!=null&&!StringUtils.isEmpty(s)){
                        specialNames.append(","+s1);
                    }else {
                        specialNames.append(s1);
                    }
                }
                if (specialNames.length()>0){
                    String substring = specialNames.substring(1, specialNames.length());
                    planManageList.setSpecialNames(substring);
                }

            }
            planManageList.setSlump(duolaPlan.getSlump());
            list.add(planManageList);
        }
        vo.setList(list);
        vo.setTotal(page.getTotal());
        return vo;
    }

    /**
     * 生产计划开始发货
     *
     * @param id
     */
    @Override
    public void planSend(Integer id) throws Exception {
        DuolaPlan duolaPlan = duolaPlanMapper.selectByPrimaryKey(id);
        if (duolaPlan==null){
            throw  new Exception( ErrorEnums.ERROR_DATA.getEmsg());
        }
        Long userId = (Long) SecurityUtils.getSubject().getPrincipal();
        duolaPlan.setUpdateTime(LocalDateTime.now());
        duolaPlan.setUpdateUid(userId);
        duolaPlan.setOrderStatus(PlanOrderStatusEnums.NOT_DRIVING.getEcode());
        duolaPlanMapper.updateByPrimaryKeySelective(duolaPlan);
        //生产计划长连接
        //发送新订单长连接
        try {
            newPlanOrderSocket.sendMessage(JSON.toJSONString(planList(null,null, null)));
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    /**
     * 根据客户id选出已绑定合同的工程
     *
     * @param customerId
     * @return
     */
    @Override
    public List<SelectProjectByCustomerIdVo> selectProjectByCustomerId(Integer customerId) {
        List<SelectProjectByCustomerIdVo> list = new ArrayList<>();
        //查询出该客户下已审批的通过的合同
        DuolaContractExample duolaContractExample = new DuolaContractExample();
        duolaContractExample.createCriteria().andCustomerIdEqualTo(customerId).andExamineStatusEqualTo(ExamineEnums.EXAMINE_SUCCESS.getEcode()).andContractTypeEqualTo(ContractTypeEnums.CONTRACT.getEcode());
        List<DuolaContractWithBLOBs> duolaContractWithBLOBs = duolaContractMapper.selectByExampleWithBLOBs(duolaContractExample);
        List<Integer> projectIds = new ArrayList<>();
        for (DuolaContractWithBLOBs duolaContractWithBLOB : duolaContractWithBLOBs) {
            projectIds.add(duolaContractWithBLOB.getProjectId());
        }
        if (projectIds.size()>0){
            DuolaProjectExample projectExample = new DuolaProjectExample();
            projectExample.createCriteria().andIdIn(projectIds);
            List<DuolaProject> duolaProjects = duolaProjectMapper.selectByExample(projectExample);
            for (DuolaProject duolaProject : duolaProjects) {
                if (duolaProject.getExamineStatus().equals(ExamineEnums.EXAMINE_SUCCESS.getEcode())){
                    SelectProjectByCustomerIdVo vo = new SelectProjectByCustomerIdVo();
                    vo.setId(duolaProject.getId());
                    vo.setProjectName(duolaProject.getProjectName());
                    list.add(vo);
                }
            }
        }
        return list;
    }

    /**
     * 删除生产计划
     *
     * @param id
     */
    @Override
    public void delPlan(Integer id) throws Exception {
        DuolaPlan duolaPlan = duolaPlanMapper.selectByPrimaryKey(id);
        if (duolaPlan==null){
            throw new Exception(ErrorEnums.ERROR_DATA.getEmsg());
        }
        if (!duolaPlan.getOrderStatus().equals(PlanOrderStatusEnums.NOT_DRIVING.getEcode())){
            throw new Exception(ErrorEnums.ERROR_UPDATE.getEmsg());
        }
        duolaPlan.setStatus(StatusEnums.YES.getEcode());
        duolaPlanMapper.updateByPrimaryKeySelective(duolaPlan);
    }

    /**
     * 查询超过30分钟还未发货的计划
     *
     * @return
     */
    @Override
    public List<DuolaPlan> findLimit() {
        LocalDateTime localDateTime = TimeTool.minTime(30);
        DuolaPlanExample example = new DuolaPlanExample();
        example.createCriteria().andOrderStatusEqualTo(PlanOrderStatusEnums.NOT_DRIVING.getEcode()).andUpdateTimeLessThanOrEqualTo(localDateTime);
        List<DuolaPlan> duolaPlans = duolaPlanMapper.selectByExample(example);
        return duolaPlans;
    }


}
